package com.wh.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 * 
 *  Class Name: SocketChannelTest.java
 *  Description: SocketChannel测试
 *  @author wanghao  DateTime 2017年1月19日 下午2:12:31 
 *  @company hongbo
 *  @email 1228675455@qq.com
 */
public class SocketChannelTest {

	public static void main(String[] args) {
/*		int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;
		System.out.println(interestSet);*/
		SocketChannel sc = null;
		Selector selector = null;
		try {
			sc = SocketChannel.open();
			sc.bind(new InetSocketAddress("127.0.0.1", 6556));
			//设置通道为不阻塞
			sc.configureBlocking(false);
			int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;
			//创建一个selector
			selector = Selector.open();
			sc.register(selector, interestSet);
			while(selector.select() > 0) {
				for(Iterator<SelectionKey> iterable = selector.selectedKeys().iterator(); iterable.hasNext();) {
					
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
